<template>
  <div>
    <el-card>
      <div slot="header"
           class="clearfix">
        <span>财务销售报表</span>
      </div>
      <el-form ref="serachForm"
                 :model="searchForm"
                 :inline="true"
                 style="width:98%"
                 label-position="left"
                 label-width="90px">
          <el-form-item prop="orderNum" label="订单号">
          			<el-input v-model="searchForm.orderNum" clearable auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="创建时间"
                        prop="timeRange">
            <el-date-picker v-model="searchForm.timeRange"
                            clearable
                            type="daterange"
                            range-separator="至"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期"></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type='primary'
                       @click="getOrderList">搜索</el-button>
          </el-form-item>
          <el-button type='primary' style="margin-left:15px"
              @click="dialogFormVisible=true">导出</el-button>
      </el-form>
      <el-form rref="serachForm"
                 :model="searchForm"
                 :inline="true"
                 style="width:98%"
                 label-position="left"
                 label-width="300px">
          <el-form-item prop="items" label="订单数量:（当前筛选条件总的订单数量）" style="width:40%;">
          			<el-input v-model="searchData.totalCount" disabled auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item prop="items" label="销售总额:（当前筛选条件总的订单金额）" style="width:40%;">
          			<el-input v-model="searchData.totalPayPrice" disabled auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item prop="items" label="物流总额:（当前筛选条件总的物流成本）" style="width:40%;">
          			<el-input v-model="searchData.totalPostage" disabled auto-complete="off"></el-input>
          </el-form-item>
           <el-form-item prop="items" label="销售利润:（当前筛选条件总的利润）" style="width:40%;">
          			<el-input v-model="searchData.totalProfitPrice" disabled auto-complete="off"></el-input>
          </el-form-item>
      </el-form>
      <el-table :data="tableData"
                style="width: 100%"
                height="400"
                stripe>
        <el-table-column prop="addTime"
                         label="订单时间"
                         align="center"></el-table-column>
        <el-table-column prop="orderNum"
                         label="订单号"
                         align="center"></el-table-column>
        <el-table-column prop="totalPrice"
                         label="订单金额"
                         align="center"></el-table-column>

        <el-table-column prop="totalPostage"
                         label="物流费用"
                         align="center"></el-table-column>
        <el-table-column prop="costPrice"
                         label="订单成本价"
                         align="center"></el-table-column>
        <el-table-column prop="profit"
                         label="订单利润"
                         align="center"></el-table-column>
        <el-table-column prop="payType"
                         label="付款方式"
                         align="center"></el-table-column>
        <el-table-column prop="status"
                         label="订单状态"
                         align="center"></el-table-column>
        <el-table-column label="操作"  align="center" fixed="right" width="120">
            <template slot-scope="scope" >
              <el-button type="primary" size="mini" @click="detail(scope.row)">详情</el-button>
             </template>
          </el-table-column>
      </el-table>
      <el-dialog title="提示"
                 :visible.sync="dialogVisible"
                 width="30%"
                 :before-close="handleClose">
        <span>是否确认订单</span>
        <span slot="footer"
              class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary"
                     @click="agreeOrder">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog title="提示"
                 :visible.sync="redialogVisible"
                 width="30%"
                 :before-close="rehandleClose">
        <span>是否打回订单</span>
        <span slot="footer"
              class="dialog-footer">
          <el-button @click="redialogVisible = false">取 消</el-button>
          <el-button type="primary"
                     @click="refuseOrder">确 定</el-button>
        </span>
      </el-dialog>
      <el-pagination style="text-align:center;margin-top:20px"
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="currentPage"
                     :page-sizes="[5, 10, 15, 20]"
                     :page-size="pageSize"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total"></el-pagination>
    </el-card>
     <el-dialog title="导出销售报表" :visible.sync="dialogFormVisible">
      <el-form :model="downLoadForm">
          <el-form-item prop="orderNum" label="订单号">
          			<el-input v-model="downLoadForm.orderNum" clearable auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="创建时间"
                        prop="timeRange">
            <el-date-picker v-model="downLoadForm.timeRange"
                            clearable
                            type="daterange"
                            range-separator="至"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期"></el-date-picker>
          </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="downLoadData">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  ORDER_AGREE, ORDER_REFUSE,ORDER_MAIN_LIST,ORDER_FINANCE_SUM,SALE_DOWNLOAD
} from '@/utils/api';
import { formatDate } from '@/utils/validate.js';
import axios from 'axios'
export default {
  data () {
    return {
      downloadUrl:"https://yzbban.oss-cn-shanghai.aliyuncs.com/",
      dialogFormVisible: false,
      downLoadForm:{
        timeRange: [new Date().getTime()-2592000000,new Date().getTime()+86400000],
        orderNum:''
      },
      searchData:{},
      searchForm:{
        isHandle:false,
        timeRange: [new Date().getTime()-2592000000,new Date().getTime()+86400000],
        orderNum:''
      },
      dialogVisible: false,
      redialogVisible: false,
      orderForm: {},
      formRule: {},
      tableData: [
        // {
        //   AKSNo: "asda112312",
        //   producerNo: "123143424234",
        //   price: "1",
        //   unit: "件",
        //   puchaseQty: 500
        // }
      ],
      payOptions: [
        {
          value: "0",
          label: "未支付"
        },
        {
          value: "1",
          label: "已支付"
        },
        {
          value: "3",
          label: "线下处理"
        }
      ],
      payMethods: [
        {
          value: "0",
          label: "银行卡"
        },
        {
          value: "1",
          label: "线下支付"
        }
      ],
      orderOptions: [
        {
          value: "0",
          label: "未打回"
        },
        {
          value: "1",
          label: "已打回"
        },
        {
          value: "2",
          label: "未确认"
        }
      ],
      isDisabled: false,
      currentPage: 1,
      pageSize: 10,
      total: 0,
    };
  },
  filters: {
    statusFilter (val) {
      console.log(val)
      return val == "待付款"
        ? true
        : val == "待收货"
          ? true
          : val == "待发货"
            ? false
            : val == "待评价"
              ? false
              : val == "已完成"
                ? false
                : val == "已评价"
                  ? false
                  : val == "退换货订单" ? false : false;
    }
  },
  mounted () {
      this.getOrderSum()
      this.getOrderList()
  },
  methods: {
    downLoadData(){
      SALE_DOWNLOAD({
        orderNum:this.downLoadForm.orderNum,
        startTime:Math.floor(new Date(this.downLoadForm.timeRange[0]).getTime()/1000),
        endTime:Math.floor(new Date(this.downLoadForm.timeRange[1]).getTime()/1000),
      }).then(res => {
        console.log(res.data)
        var url = this.downloadUrl + res.data
        location.href = url
        this.dialogFormVisible = false
      })
    },
    detail (row, index) {
      this.$router.push({
        path: "salesReportDetail",
        query: {
          data: row,
          handler: "check"
        }
      });
    },
    // 确认发货
    sendPro (val) {
      console.log(val)
      let th = this
      if (val.status == "待付款") {
        val.status = 0
      } else if (val.status == "待收货") {
        val.status = 1
      } else if (val.status == "待发货") {
        val.status = 2
      } else if (val.status == "待评价") {
        val.status = 3
      } else if (val.status == "已评价") {
        val.status = 6
      } else if (val.status == "退换货订单") {
        val.status = -1
      } else if (val.status == "已完成") {
        val.status = 4
      }
      console.log(val.status, val.unique)
      let param = {
        uni: val.unique,
        status: 1
      }  // 创建form对象
      let config = {
        headers: { 'Content-Type': 'application/json' }
      }
      axios.post('http://fox.foxcore.cn/api/v1/redirect/adminConfirm', param, config).then(res => {
        this.getOrderList()
      })
    },
    rehandleClose (done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => { });
    },
    handleClose (done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(res => { });
    },
    refuseOrder () {
      ORDER_REFUSE({
        orderNum: this.orderForm.orderId,
      }).then(res => {
        this.$message.success('打回操作成功')
        this.getOrderList()
        this.redialogVisible = false
      })
    },
    agreeOrder () {
      ORDER_AGREE({
        orderNum: this.orderForm.orderId,
      }).then(res => {
        this.$message.success('同意操作成功')
        this.getOrderList()
        this.dialogVisible = false
      })
    },
    getOrderSum(){
      ORDER_FINANCE_SUM({
        orderNum:this.searchForm.orderNum,
        startTime:Math.floor(new Date(this.searchForm.timeRange[0]).getTime()/1000),
        endTime:Math.floor(new Date(this.searchForm.timeRange[1]).getTime()/1000),
      }).then(res=>{
        this.searchData=res.data
      })
    },
    getOrderList () {
       //退换货
       ORDER_MAIN_LIST({ 
          orderNum:this.searchForm.orderNum,
          startTime:Math.floor(new Date(this.searchForm.timeRange[0]).getTime()/1000),
          endTime:Math.floor(new Date(this.searchForm.timeRange[1]).getTime()/1000),
          pageNo: this.currentPage,
          pageSize: this.pageSize
      }).then(res => {
          console.log(res.data.dataList)
          this.tableData = res.data.dataList
          this.total = res.data.count
          for (let i in this.tableData) {
            this.tableData[i].addTime = this.formatSecond(this.tableData[i].addTime)
            if(this.tableData[i].payType==10){
              this.tableData[i].payType = "线下"
            }else{
              this.tableData[i].payType = "线上"
            }
            //0：待付款；1：待收货；2：待发货；3：待评价；4：已完成；6 已评价 
            //状态
            switch(this.tableData[i].status){
              case 0:
                this.tableData[i].status = "待付款"
                break
              case 1:
                this.tableData[i].status = "待收货"
                break;
              case 2:
                this.tableData[i].status = "待发货"
                break;
              case 3:
                this.tableData[i].status = "待评价"
                break;
              case 6:
                this.tableData[i].status = "已评价"
                break;
              case -1:
                this.tableData[i].status = "退换货订单"
                break;
              case 4:
                this.tableData[i].status = "已完成"
                break;
            }
          }
      })
    },
    //时间格式化  
    formatSecond (second) {
      if (!second) return "";
      return formatDate(new Date(second * 1000), "yyyy-MM-dd hh:mm:ss");
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val
      this.getOrderList()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val
      this.getOrderList()
    }
  }
};
</script>

<style lang="scss" scoped>
</style>